/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package View;

import Model.Board;
import Model.Downer;
import Model.Square;
import Model.Upper;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import javax.swing.*;

/**
 *
 * @author tg300
 */
public class BoardCreator extends JLayeredPane {

    private int defRows = 10;
    private int defCols = 10;
    private ArrayList<SquareView> squarePanel;
    private Board backBoard;
    private static JPanel mainPanel = new JPanel();
    private static JPanel middleSidePanel = new JPanel();
    private static JPanel miniSidePanel = new JPanel();
    private static JPanel sidePanel = new JPanel();
    private static JButton save;
    private static JButton help;
    private static JButton quit;
    private static JButton roller;
    private static JButton moveFor;
    private static JButton moveBack;
    private static JLabel gameName1 = new JLabel("  Uppers And Downers");
    private static JLabel humanScore = new JLabel("Human Score     ");
    private static JLabel compScore = new JLabel("CPU Score         ");
    private static JLabel humanPos = new JLabel("Human Position");
    private static JLabel humanPosLabel;
    private static JLabel compPosLabel;
    private static JLabel compPos = new JLabel("CPU Position     ");
    private static Font defFont = new Font("Dialog", Font.PLAIN, 20);
    private ImageIcon token;
    private ImageIcon token2;
    private int HumanPosTracker;
    private int CompPosTracker;

    public BoardCreator(int rows, int cols, ArrayList<Integer> ups, ArrayList<Integer> downs) {
        
        defRows = rows;
        defCols = cols;
        squarePanel = new ArrayList(defRows*defCols);

        JPanel squareContainer = new JPanel();
        squareContainer.setLayout(new GridLayout(defRows, defCols));

        boolean checker = true;
        int panelTracker = 0;
        int squareCounter = defRows*defCols;

        for(int i = defRows; i > 0; i--) {
            for(int j = 1; j <= defCols; j++) {

                squarePanel.add(new SquareView(panelTracker, squareCounter));
                squareContainer.add(squarePanel.get(panelTracker));

                panelTracker++;

                            if(checker == true && j <= (defCols - 1))
                                squareCounter--;
                            else if(checker == false && j <= (defCols - 1))
                                squareCounter++;
            }
                    if(checker == true)
                        checker = false;
                    else
                        checker = true;
                    
                squareCounter = squareCounter - (defCols);

        }

        squareContainer.setBounds(0, 0, 700, 700);
        this.add(squareContainer, new Integer(1));
        this.setVisible(true);
       
        /*
         * Adding buttons, scores and positions for the right hand side of the board.
         */

        save = new JButton("SAVE GAME"); save.setBackground(Color.green); save.setFont(defFont);
        help = new JButton("HELP?"); help.setBackground(Color.orange); help.setFont(defFont);
        quit = new JButton("QUIT"); quit.setBackground(Color.red); quit.setFont(defFont);
        roller = new JButton("ROLL!"); roller.setBackground(Color.yellow); roller.setFont(defFont);
        moveFor = new JButton("For"); moveFor.setBackground(Color.yellow); moveFor.setFont(defFont); moveFor.setEnabled(false);
        moveBack = new JButton("Back"); moveBack.setBackground(Color.yellow); moveBack.setFont(defFont); moveBack.setEnabled(false);
        gameName1.setForeground(Color.red);
        gameName1.setFont(new Font("Dialog", Font.BOLD, 15));

        
        sidePanel.setLayout(new GridLayout(3,1,10,10));
        sidePanel.setBackground(new Color(0, 0, 128));
        miniSidePanel.setLayout(new GridLayout(5,1));
        miniSidePanel.setBackground(new Color(0, 0, 128));
        miniSidePanel.add(gameName1);
        middleSidePanel.setBackground(new Color(0, 0, 128));
        middleSidePanel.setLayout(new GridLayout(4,1,10,10));

            JPanel humanScorePanel = new JPanel();
            humanScorePanel.setBackground(new Color(0, 0, 128));
            JPanel compScorePanel = new JPanel();
            compScorePanel.setBackground(new Color(0, 0, 128));

                JLabel humanDisplayLabel = new JLabel("    0");
                humanDisplayLabel.setForeground(Color.white);
                humanDisplayLabel.setPreferredSize(new Dimension(40,40));
                humanScore.setForeground(Color.white);
                humanScore.setFont(new Font("Dialog", Font.BOLD, 15));
                humanScorePanel.add(humanScore);
                humanScorePanel.add(humanDisplayLabel);
                miniSidePanel.add(humanScorePanel);

                JLabel compDisplayLabel = new JLabel("    0");
                compDisplayLabel.setForeground(Color.white);
                compDisplayLabel.setPreferredSize(new Dimension(40,40));
                compScorePanel.add(compScore);
                compScore.setForeground(Color.white);
                compScore.setFont(new Font("Dialog", Font.BOLD, 15));
                compScorePanel.add(compDisplayLabel);
                miniSidePanel.add(compScorePanel);

            JPanel humanPosPanel = new JPanel();
            humanPosPanel.setBackground(new Color(0, 0, 128));
            JPanel compPosPanel = new JPanel();
            compPosPanel.setBackground(new Color(0, 0, 128));

                humanPosLabel = new JLabel("    1");
                humanPosLabel.setForeground(Color.white);
                humanPosLabel.setPreferredSize(new Dimension(40,40));
                humanPos.setForeground(Color.white);
                humanPos.setFont(new Font("Dialog", Font.BOLD, 15));
                humanPosPanel.add(humanPos);
                humanPosPanel.add(humanPosLabel);
                miniSidePanel.add(humanPosPanel);

                compPosLabel = new JLabel("    1");
                compPosLabel.setForeground(Color.white);
                compPosLabel.setPreferredSize(new Dimension(40,40));
                compPos.setForeground(Color.white);
                compPos.setFont(new Font("Dialog", Font.BOLD, 15));
                compPosPanel.add(compPos);
                compPosPanel.add(compPosLabel);
                miniSidePanel.add(compPosPanel);

        middleSidePanel.add(save);
        middleSidePanel.add(help);
        middleSidePanel.add(quit);
        middleSidePanel.add(roller);

            JPanel movement = new JPanel(new FlowLayout());
            movement.setBackground(new Color(0, 0, 128));
            movement.add(moveBack);
            movement.add(moveFor);
        
        //add the mini side panel here
        sidePanel.add(miniSidePanel);
        //add the empty panel here
        sidePanel.add(middleSidePanel);
        //add roll panel here
        sidePanel.add(movement);
        
        mainPanel.setLayout(new BorderLayout());
        mainPanel.add(this, BorderLayout.CENTER);
        mainPanel.add(sidePanel, BorderLayout.EAST);
        mainPanel.setBackground(new Color(0, 0, 128));

        /*
         * Create frame for the whole board and add the main panel.
         */
        
        JFrame frame = new JFrame("Group I");
        frame.getContentPane().add(mainPanel);
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {System.exit(0); }
            public void windowDeiconified(WindowEvent e) { ; }
            public void windowIconified(WindowEvent e) { ; }

        });

        frame.pack();
        frame.setVisible(true);
        frame.setSize(900,727);
        frame.setResizable(false);
       
        /*
         * Set up uppers and downers and create command line board.
         */
        
       backBoard = new Board(rows*cols, ups, downs);
       
        /*
         * Convert the numbers picked by the users to the actual arraylist value of the squares created
         */
       ArrayList<Integer> realUps = ups;
       ArrayList<Integer> realDowns = downs;
       
        for(int i = 0; i < realUps.size(); i++) {
            for(int j = 0; j < squarePanel.size(); j++) {    
                if(realUps.get(i) == squarePanel.get(j).getView()) {
                    realUps.set(i, j);
                    break;
                }
            }
        }
        
        for(int i = 0; i < realDowns.size(); i++) {
            for(int j = 0; j < squarePanel.size(); j++) {    
                if(realDowns.get(i) == squarePanel.get(j).getView()) {
                    realDowns.set(i, j);
                    break;
                }
            }
        }
        
        /*
         * Create int arrays to store the x and y co-ordinate of the uppers and downers
         */
        
        int[] upX = new int[10]; int[] upY = new int[10];
        int[] downX = new int[10]; int[] downY = new int[10];
        
        for (int i = 0; i < realUps.size(); i++) {
            upX[i] = squarePanel.get(realUps.get(i)).getX()  + 35;
            upY[i] = squarePanel.get(realUps.get(i)).getY()  + 35;
        }
        
        for (int i = 0; i < realDowns.size(); i++) {
            downX[i] = squarePanel.get(realDowns.get(i)).getX() + 35;
            downY[i] = squarePanel.get(realDowns.get(i)).getY() + 35;
        }
        
        UppersAndDowners uad = new UppersAndDowners(upX, upY, downX, downY);
        uad.setBounds(0,0,700,700);
        this.add(uad, new Integer(2));

        ImageIcon tempToken = new ImageIcon(getClass().getResource("token.PNG"));
        Image img = tempToken.getImage().getScaledInstance(70, 65, java.awt.Image.SCALE_SMOOTH);
        token = new ImageIcon(img);
        
        ImageIcon tempToken2 = new ImageIcon(getClass().getResource("token2.PNG"));
        Image img2 = tempToken2.getImage().getScaledInstance(70, 65, java.awt.Image.SCALE_SMOOTH);
        token2 = new ImageIcon(img2);
        
        setStartImage();
        
    }

    public void setStartImage() {
        //JLabel lab = new JLabel("");
        //lab.setIcon(token);
        int firstIcon = actual(1);
        HumanPosTracker = firstIcon;
        CompPosTracker = firstIcon;
        squarePanel.get(firstIcon).setText(null);
        squarePanel.get(firstIcon).setIcon(token);
    }

    public void addBoardListener(ActionListener mal) {
        save.addActionListener(mal);
        help.addActionListener(mal);
        quit.addActionListener(mal);
        roller.addActionListener(mal);
    }

    public void addMoveListener(ActionListener mal2) {
        moveFor.addActionListener(mal2);
        moveBack.addActionListener(mal2);
    }

    public void setMove(boolean m) {
        moveFor.setEnabled(m);
        moveBack.setEnabled(m);
    }

    public void setRollButton(boolean n) {
        roller.setEnabled(n);
    }
    
    public void setHumanToken(int pos) {
        int realPos = actual(pos);
        squarePanel.get(HumanPosTracker).setIcon(null);
        squarePanel.get(HumanPosTracker).setText("         " + squarePanel.get(HumanPosTracker).getView());
        HumanPosTracker = realPos;
        squarePanel.get(realPos).setText(null);
        squarePanel.get(realPos).setIcon(token);
        
    }
    
    public void setCompToken(int pos) {
        int realPos = actual(pos);
        squarePanel.get(CompPosTracker).setIcon(null);
        squarePanel.get(CompPosTracker).setText("         " + squarePanel.get(CompPosTracker).getView());
        CompPosTracker = realPos;
        squarePanel.get(realPos).setText(null);
        squarePanel.get(realPos).setIcon(token2);
    }
    
    public void setHumanText() {
        humanPosLabel.setText("    " + squarePanel.get(HumanPosTracker).getView());
    }
    
    public void setCompText() {
        compPosLabel.setText("    " + squarePanel.get(CompPosTracker).getView());
    }

    public int actual(int a) {
        for(int i = 0; i < squarePanel.size(); i++) {
            if(a == squarePanel.get(i).getView()) {
                return i;
            }

        }
        return 0;
    }
    
}